/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.datamonitor.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.datamonitor.entity.MonitorPolice;
import com.jeeplus.modules.datamonitor.service.MonitorPoliceService;

/**
 * 设备检测报警Controller
 * @author 刘明星
 * @version 2020-12-11
 */
@RestController
@RequestMapping(value = "/datamonitor/monitorPolice")
public class MonitorPoliceController extends BaseController {

	@Autowired
	private MonitorPoliceService monitorPoliceService;

	@ModelAttribute
	public MonitorPolice get(@RequestParam(required=false) String id) {
		MonitorPolice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = monitorPoliceService.get(id);
		}
		if (entity == null){
			entity = new MonitorPolice();
		}
		return entity;
	}


	/**
	 * 设备检测报警树表数据
	 */
	@GetMapping("list")
	public AjaxJson list(MonitorPolice monitorPolice) {
		return AjaxJson.success().put("list", monitorPoliceService.findList(monitorPolice));
	}

	/**
	 * 根据Id获取设备检测报警数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(MonitorPolice monitorPolice) {
		return AjaxJson.success().put("monitorPolice", monitorPolice);
	}

	/**
	 * 保存设备检测报警
	 */
	@PostMapping("save")
	public AjaxJson save(MonitorPolice monitorPolice, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(monitorPolice);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		monitorPoliceService.save(monitorPolice);//保存
		return AjaxJson.success("保存设备检测报警成功");
	}

	/**
	 * 删除设备检测报警
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(MonitorPolice monitorPolice) {
		monitorPoliceService.delete(monitorPolice);
		return AjaxJson.success("删除设备检测报警成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<MonitorPolice> list = monitorPoliceService.findList(new MonitorPolice());
		List rootTree = getMonitorPoliceTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<MonitorPolice> getMonitorPoliceTree(List<MonitorPolice> list, String extId) {
		List<MonitorPolice> monitorPolices = Lists.newArrayList();
		List<MonitorPolice> rootTrees = monitorPoliceService.getChildren("0");
		for (MonitorPolice root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        monitorPolices.add(getChildOfTree(root, list, extId));
		    }
		}
		return monitorPolices;
	}

	private MonitorPolice getChildOfTree(MonitorPolice monitorPolice, List<MonitorPolice> monitorPoliceList, String extId) {
		monitorPolice.setChildren(Lists.newArrayList());
		for (MonitorPolice child : monitorPoliceList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(monitorPolice.getId())) {
		            monitorPolice.getChildren().add(getChildOfTree(child, monitorPoliceList, extId));
		        }
		    }
		}
		return monitorPolice;
	}



}